package com.taiyo.modules.api.controller;

import com.alibaba.fastjson.JSON;
import com.taiyo.common.utils.R;
import com.taiyo.modules.api.entity.TIdentityEntity;
import com.taiyo.modules.api.entity.TIdentityItemEntity;
import com.taiyo.modules.api.entity.TProductEntity;
import com.taiyo.modules.api.entity.app.IdentityRequset;
import com.taiyo.modules.api.service.TDiscountService;
import com.taiyo.modules.api.service.TIdentityItemService;
import com.taiyo.modules.api.service.TIdentityService;
import com.taiyo.modules.api.service.TProductService;
import com.taiyo.modules.sys.controller.AbstractController;
import com.taiyo.modules.sys.entity.SysUserEntity;
import com.taiyo.modules.sys.service.ShiroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/1/15.
 */
@RestController
@RequestMapping("/api/identity")
public class IdentityController extends AbstractController {

    @Autowired
    private TIdentityItemService tIdentityItemService;
    @Autowired
    private TProductService tProductService;
    @Autowired
    private TIdentityService tIdentityService;
    @Autowired
    private TDiscountService tDiscountService;
    @Autowired
    private ShiroService shiroService;

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestParam Map<String, Object> params){
        SysUserEntity sysUserEntity=shiroService.queryByToken(params.get("token").toString());
        IdentityRequset identityRequset=new IdentityRequset();
        identityRequset.settIdentityEntity(JSON.parseObject((String) params.get("tIdentityEntity"),TIdentityEntity.class));
        identityRequset.setTypeList(JSON.parseObject((String) params.get("typeList"),List.class));
        identityRequset.setPriceList(JSON.parseObject((String) params.get("priceList"),List.class));
        tIdentityService.save(identityRequset.gettIdentityEntity());
        long identityId =identityRequset.gettIdentityEntity().getId();
        List<Long> typelist=identityRequset.getTypeList();
        List<Double> pricelist=identityRequset.getPriceList();
        for(int i=0;i<typelist.size();i++){
            TProductEntity tProductEntity=new TProductEntity();
            tProductEntity.setCategoryId(identityRequset.gettIdentityEntity().getCategoryId());
            tProductEntity.setDescription(identityRequset.gettIdentityEntity().getDescriptionImg());
            tProductEntity.setImg(identityRequset.gettIdentityEntity().getImg());
            tProductEntity.setLocation(identityRequset.gettIdentityEntity().getLocation());
            tProductEntity.setName(identityRequset.gettIdentityEntity().getTitle());
            tProductEntity.setStyleId(identityRequset.gettIdentityEntity().getStyleIds());
            tProductEntity.setImg(identityRequset.gettIdentityEntity().getImg());
            Object pri=pricelist.get(i);
            Object t=typelist.get(i);
            double price=Double.parseDouble(pri.toString());
            tProductEntity.setPrice(price);
            long type=Long.parseLong(t.toString());
            tProductEntity.setTypeId(type);
            tProductEntity.setUserId(sysUserEntity.getUserId());
            tProductEntity.setIdentityId(identityId);
            tProductEntity.setIstj(0);//默认不推荐
            long productId=tProductService.save(tProductEntity);
            TIdentityItemEntity tIdentityItemEntity=new TIdentityItemEntity();
            tIdentityItemEntity.setIdentityId(identityId);
            tIdentityItemEntity.setPrice(price+"");
            tIdentityItemEntity.setTypeId(type);
            tIdentityItemEntity.setProductId(productId);
            tIdentityItemService.save(tIdentityItemEntity);
        }
        return R.ok();
    }
    @RequestMapping("/list")
    public R List(@RequestParam Map<String, Object> params){
        Map<String,Object> map=new HashMap<>();
        SysUserEntity sysUserEntity=shiroService.queryByToken((String) params.get("token"));
        map.put("user_id",sysUserEntity.getUserId());
        List<IdentityRequset> identityRequsets=new ArrayList<>();
        List<TIdentityEntity> tIdentityEntities=tIdentityService.queryList(map);
        for(TIdentityEntity tIdentityEntity:tIdentityEntities){
            IdentityRequset identityRequset=new IdentityRequset();
            identityRequset.settIdentityEntity(tIdentityEntity);
            List<TIdentityItemEntity> tIdentityItemEntities=tIdentityItemService.queryList(null);
            List<Double> price=new ArrayList<>();
            List<Long> types=new ArrayList<>();
            for(TIdentityItemEntity tIdentityItemEntity:tIdentityItemEntities){
                price.add(Double.valueOf(tIdentityItemEntity.getPrice()));
                types.add(tIdentityItemEntity.getTypeId());
            }
            identityRequset.setPriceList(price);
            identityRequset.setTypeList(types);
            identityRequsets.add(identityRequset);
        }
        return R.ok().put("page", identityRequsets);
    }

}
